'use strict';
import React from "react";
import {View, StyleSheet, TextInputComponent, TextInput} from "react-native";
import Container from "../../component/common/Container";
import NavigationBar from "../../component/navigation/NavigationBar";
import {KeyboardAwareScrollView} from "react-native-keyboard-aware-scroll-view";
import Button from "../../component/common/Button";

class PublishNote extends React.Component {

    constructor(props) {
        super(props);
        this.inputData = {title:'', content:''}
    }

    _onPressPublish = async () => {
        const {params} = this.props.navigation.state;
        const {callBack} = params;
        if (!this.inputData['title'] || !this.inputData['content']) {
            ToastManager.show('必填项不能为空!');
            return;
        }
        const data = {
            topic_title: this.inputData['title'],
            topic_content: this.inputData['content'],
            create_user: global.secretKey,
        };
        const result = await Services.post(ServicesApi.PUBLISH_FORUM,data);
        if (result.code === StatusCode.SUCCESS_CODE) {
            ToastManager.show('发布成功!');
            callBack && callBack();
            RouterHelper.goBack();
        }else {
            ToastManager.show(result.dec || 'error')
        }
    };

    onChangeText = (type,content) => {
        this.inputData[type] = content;

    };

    render() {

        const {params} = this.props.navigation.state;
        const pageTitle = params.pageTitle || '发布帖子';

        return (
            <Container>
                <NavigationBar
                    title={pageTitle}
                />
                <KeyboardAwareScrollView
                    style={styles.scroll}
                    keyboardShouldPersistTaps={'handled'}
                >
                    <TextInput
                        style={styles.inputTitle}
                        numberOfLines={1}
                        maxLength={12}
                        placeholder={'请输入标题'}
                        onChangeText={(text) => this.onChangeText('title',text)}
                    />
                    <TextInput
                        style={styles.inputContent}
                        multiline={true}
                        maxLength={400}
                        placeholder={'请输入帖子内容'}
                        onChangeText={(text) => this.onChangeText('content',text)}
                    />
                    <Button
                        style={styles.publishButton}
                        titleStyle={styles.titleButton}
                        title={'发布帖子'}
                        onPress={this._onPressPublish}
                    />
                </KeyboardAwareScrollView>
            </Container>
        );
    }
}

const styles = StyleSheet.create({
    scroll: {
        flex: 1,
    },
    inputTitle: {
        padding: 5,
        fontSize: FontSize(16),
        color: "#333",
        marginHorizontal: 15,
        marginTop: 22,
        borderRadius: 3,
        borderWidth: Theme.minPixel,
        borderColor: "#dbdbdb",
    },
    inputContent: {
        padding: 5,
        fontSize: FontSize(12),
        color: "#333",
        textAlignVertical: 'top',
        marginHorizontal: 15,
        marginTop: 12,
        borderRadius: 3,
        borderWidth: Theme.minPixel,
        borderColor: "#dbdbdb",
        height: 197,
    },
    publishButton: {
        marginTop: 20,
        borderRadius: 3,
        backgroundColor: "#ffa64f",
        paddingVertical: 10,
        paddingHorizontal: 66,
        alignSelf: 'center',
    },
    titleButton: {
        fontSize: FontSize(16),
        color: "#ffffff",
    },
});

export default PublishNote